package de.voicegym.voicegym.model

import androidx.lifecycle.LiveData
import androidx.room.*
import androidx.room.OnConflictStrategy.REPLACE
import java.util.*

@Entity(tableName = "recordings")
data class Recording(
    @PrimaryKey(autoGenerate = true)
    var id: Long?,
    @ColumnInfo(name = "fileName")
    var fileName: String,
    @ColumnInfo(name = "duration")
    var duration: Int,
    @ColumnInfo(name = "created_at")
    var createdAt: Long,
    @ColumnInfo(name = "updated_at")
    var updatedAt: Long,
    @ColumnInfo(name = "rating")
    var rating: Int,
    @ColumnInfo(name = "title")
    var title: String?
) {

    constructor() : this(
        null,
        "",
        0,
        Calendar.getInstance().timeInMillis,
        Calendar.getInstance().timeInMillis,
        0,
        null
    )
}

@Dao
interface RecordingDao {

    @Query("SELECT * from recordings")
    fun getAll(): LiveData<List<Recording>>

    @Insert(onConflict = REPLACE)
    fun insert(recording: Recording)

    @Query("Select * from recordings where filename=:filename")
    fun getByFileName(filename: String): Recording?

    @Delete
    fun delete(recording: Recording)
}
